package cn.itcast.p2p20.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import cn.itcast.p2p20.R;
import cn.itcast.p2p20.adapter.PicAdapter;
import cn.itcast.p2p20.utils.Util;

public class GuideActivity extends BaseActivity {

    public static final int IMAGE_RES_IDS_OUT[] = {R.drawable.guiding_img_1, R.drawable.guiding_img_2, R.drawable.guiding_img_3, R.drawable.guiding_img_4};
    public static final int IMAGE_RES_IDS_INNER[] = {R.drawable.g1, R.drawable.g2, R.drawable.g3, R.drawable.g4};

    private ViewPager mViewPagerOut;
    private ViewPager mViewPagerInner;
    private Button mStartBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        mViewPagerOut = ((ViewPager) findViewById(R.id.viewpager_out));
        mViewPagerInner = ((ViewPager) findViewById(R.id.viewpager_inner));
        mStartBt = ((Button) findViewById(R.id.bt_start));

        //会压缩触摸滑动的敏感度
        mViewPagerInner.setScaleX(0.5f);
        mViewPagerInner.setScaleY(0.5f);

        mViewPagerOut.setAdapter(new PicAdapter(IMAGE_RES_IDS_OUT));
        mViewPagerInner.setAdapter(new PicAdapter(IMAGE_RES_IDS_INNER));

        mViewPagerOut.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                mViewPagerInner.onTouchEvent(event);
                return false;
            }
        });

        mViewPagerInner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                mViewPagerOut.onTouchEvent(event);
                return false;
            }
        });

        mViewPagerOut.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

                if (position == IMAGE_RES_IDS_OUT.length - 1) {

                    mStartBt.setVisibility(View.VISIBLE);
                } else {

                    mStartBt.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    //点击进入主界面
    public void start(View view) {

        Util.hasStartGuideToMainUI(this);
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
